#ifndef CORS_MIDDLEWARE_H
#define CORS_MIDDLEWARE_H

#include "../Middleware.h"
#include "./CorsConfig.h"

class CorsMiddleware : public Middleware
{
public:
    explicit CorsMiddleware(const CorsConfig &config) : config_(config) {};

    void before(HttpRequest &request) override;
    void after(HttpResponse &response) override;

private:
    void handleOptionsRequest(const HttpRequest &request, HttpResponse &response);
    bool isOriginAllowed(const std::string &origin) const;
    void addCorsHeaders(HttpResponse &response, const std::string &origin);
    std::string joinStr(const std::vector<std::string> &strings, const std::string &delimiter); // 拼接字符串

private:
    CorsConfig config_ = CorsConfig::getDefaultConfig();
};

#endif // CORS_MIDDLEWARE_H